package com.pccw.usercenterservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pccw.usercenterdao.entity.SysUser;
import com.pccw.usercenterdao.mapper.SysUserMapper;
import com.pccw.usercenterservice.dto.EmailDTO;
import com.pccw.usercenterservice.dto.SysUserDTO;
import com.pccw.usercenterservice.dto.UserRegisterDTO;
import com.pccw.usercenterservice.exception.PccwException;
import com.pccw.usercenterservice.service.EmailService;
import com.pccw.usercenterservice.service.SysUserService;
import com.pccw.usercenterservice.utils.RsaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Objects;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Resource
    SysUserMapper sysUserMapper;

    @Value("${rsa.private-key}")
    private String privateKey;

    @Autowired
    EmailService emailService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean register(UserRegisterDTO userRegisterDTO) {
        checkHasRegisted(userRegisterDTO.getEmail());
        SysUser sysUser = new SysUser();
        sysUser.setEmail(userRegisterDTO.getEmail());
        try {
            sysUser.setPassWord(RsaUtils.encryptByPrivateKey(privateKey, userRegisterDTO.getPassword()));
        }catch (Exception e){
            throw new PccwException("注册密码加密错误");
        }
        sysUser.setUserName(userRegisterDTO.getUserName());
        sysUserMapper.insert(sysUser);
        EmailDTO emailDTO = new EmailDTO();
        emailDTO.setContent("pccw账号创建成功，请登录使用！");
        emailDTO.setSubject("success");
        emailDTO.setTos(Arrays.asList(userRegisterDTO.getEmail()));
        emailService.send(emailDTO);
        return true;
    }

    private void checkHasRegisted(String email) {
        QueryWrapper<SysUser> sysUserQueryWrapper = new QueryWrapper<>();
        sysUserQueryWrapper.lambda().eq(SysUser::getEmail,email);
        SysUser sysUser = sysUserMapper.selectOne(sysUserQueryWrapper);
        if(Objects.nonNull(sysUser)){
            throw new PccwException("emial已经注册过了");
        }
    }

    @Override
    public SysUserDTO getById(String id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        if(Objects.isNull(sysUser)){
            throw new PccwException("查询用户不存在");
        }
        SysUserDTO sysUserDTO = new SysUserDTO();
        sysUserDTO.setId(String.valueOf(sysUser.getId()));
        sysUserDTO.setEmail(sysUser.getEmail());
        sysUserDTO.setUserName(sysUser.getUserName());
        return sysUserDTO;
    }

    @Override
    public Integer updateById(SysUserDTO sysUserDTO) {
        SysUser sysUser = sysUserMapper.selectById(sysUserDTO.getId());
        if(Objects.isNull(sysUser)){
            throw new PccwException("查询用户不存在");
        }
        checkHasRegisted(sysUserDTO.getEmail());
        SysUser sysUser1 = new SysUser();
        sysUser1.setId(Long.parseLong(sysUserDTO.getId()));
        sysUser1.setEmail(sysUserDTO.getEmail());
        sysUser1.setUserName(sysUserDTO.getUserName());
        return sysUserMapper.updateById(sysUser1);
    }

    @Override
    public Integer deleteById(String id) {
        SysUser sysUser = sysUserMapper.selectById(id);
        if(Objects.isNull(sysUser)){
            throw new PccwException("查询用户不存在");
        }
        return sysUserMapper.deleteById(id);
    }
}
